package mysql

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Account   string `gorm:"type:char(11);not null;comment:'手机号'"`
	Password  string `gorm:"type:char(32);not null;comment:'密码'"`
	UserImage string `gorm:"type:varchar(500);not null;comment:'用户头像'"`
	Info      string `gorm:"type:varchar(50);not null;comment:'简介'"`
	NickName  string `gorm:"type:varchar(50);not null;comment:'用户昵称'"`
}
type Goods struct {
	gorm.Model
	UserId     int     `gorm:"type:int;not null;index;comment:'用户id'"`
	GoodsTitle string  `gorm:"type:varchar(100);not null;comment:'商品标题'"`
	GoodsPrice float64 `form:"type:decimal(10,2);not null;comment:'商品价格'"`
	Stock      int     `gorm:"type:int;not null;comment:'库存'"`
	GoodsCover string  `gorm:"type:varchar(255);not null;comment:'商品封面图片'"`
	Status     int     `gorm:"type:int;not null;comment:'商品状态:上架 1 下架 0 '"`
	CheckStaus int     `gorm:"type:int;not null;comment:'审核状态 0 审核中 1 审核通过 2 审核不通过 3'"`
}
type RealName struct {
	gorm.Model
	IDCard       string `gorm:"type:char(18);not null;comment:'身份证号'"`
	RealNameAuth string `gorm:"type:varchar(30);not null;comment:'真实姓名'"`
	UserId       int    `gorm:"type:int(10);not null;comment:'用户id'"`
}

func (g *Goods) Create() error {
	return DB.Create(&g).Error
}

func (g *Goods) GetGoodsInfoById(goodsId int64) error {
	return DB.Where("id=?", goodsId).Limit(1).Find(&g).Error
}
func (g *Goods) GetList(db *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
